﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

using GameCommon.ComponentInterfaces;

using Microsoft.Xna.Framework;

namespace GameCommon.Components
{
    public class AnimationCollection : IEnumerable<IAnimation>, IUpdatable
    {
        private readonly List<IAnimation> animations;

        public AnimationCollection()
        {
            this.animations = new List<IAnimation>();
        }

        IEnumerator<IAnimation> IEnumerable<IAnimation>.GetEnumerator()
        {
            return this.animations.GetEnumerator();
        }

        public IEnumerator GetEnumerator()
        {
            return this.animations.GetEnumerator();
        }

        public void Add(IAnimation animation)
        {
            this.animations.Add(animation);
        }

        public void AddRange(IEnumerable<IAnimation> animationsToAdd)
        {
            this.animations.AddRange(animationsToAdd);
        }

        public void Update(GameTime gameTime)
        {
            foreach (var animation in this.animations.ToList())
            {
                if (animation.IsFinished)
                {
                    this.animations.Remove(animation);
                }

                animation.Update(gameTime);
            }
        }

        public void RemoveAll(Predicate<IAnimation> predicate)
        {
            this.animations.RemoveAll(predicate);
        }

        public bool IsFinished()
        {
            return !this.animations.Any();
        }
    }
}